package com.tigger.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tigger.core.mapper.EsFileMapper;
import com.tigger.core.po.EsFilePo;
import com.tigger.core.redis.lock.annotation.CacheLock;
import com.tigger.core.service.IEsFileBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("esFileBiz")
public class EsFileBizImpl implements IEsFileBiz {

    @Autowired
    private EsFileMapper mapper;

    @Override
    @Transactional(readOnly = true)
    @Cacheable(key = "'file_' + #fileId", value = "commonCache", sync = true)
    public EsFilePo findById(String fileId) {
        return mapper.selectByPrimaryKey(fileId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void save(EsFilePo file) {
        mapper.insert(file);
    }

    @Override
    @Transactional(readOnly = true)
    @CacheLock(lockedPrefix = "ES_FILE", expireTime = 500, timeOut = 1000)
    public List<EsFilePo> findByPage(Map<String, Object> params) {
        int pageNum = (Integer) params.get("pageNum");
        int pageSize = (Integer) params.get("pageSize");
        PageHelper.startPage(pageNum, pageSize, true);
        PageInfo<EsFilePo> page = new PageInfo<>(mapper.selectByPage(params));
        return page.getList();
    }


}
